package com.lps.hello.controller;

import com.lps.pojo.User;
import com.lps.util.http.RequestUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

@Controller
public class HelloController {

    @GetMapping(value = "/hello")
    @ResponseBody
    public String hello() {
        return "lps, hello!";
    }

    @GetMapping(value = "/user")
    @ResponseBody
    public User user() {
        User user = new User();
        user.setId(1);
        user.setUsername("lps");
        user.setSex("男");
        user.setAddress("北京");
        user.setPhone("13811111111");
        return user;
    }

    @GetMapping(value = "/user/a/b")
    @ResponseBody
    public User user1() {
        User user = new User();
        user.setId(1);
        user.setUsername("lps");
        user.setSex("男");
        user.setAddress("北京");
        user.setPhone("13811111111");
        return user;
    }
    @GetMapping(value = "/user/123/Boss")
    @ResponseBody
    public User user2() {
        User user = new User();
        user.setId(1);
        user.setUsername("lps");
        user.setSex("男");
        user.setAddress("北京");
        user.setPhone("13811111111");
        return user;
    }

    @GetMapping(value = "/printGetParams")
    @ResponseBody
    public Object printGetParams(ServletRequest request) {
        return RequestUtils.getRequestLinkedParams(request);
    }

    @PostMapping(value = "/printPostParams")
    @ResponseBody
    public Object printPostParams(ServletRequest request) {
        return RequestUtils.getRequestLinkedParams(request);
    }

    @PostMapping(value = "/printPostBody")
    @ResponseBody
    public Object printPostBody(ServletRequest request) {
        return RequestUtils.getRequestBodyString(request);
    }

    @GetMapping(value = "/get500")
    @ResponseBody
    public Object returnGet500() {
        throw new IllegalArgumentException("500");
    }

    @PostMapping(value = "/post500")
    @ResponseBody
    public Object returnPost500() {
        throw new IllegalArgumentException("500");
    }

    @GetMapping(value = "/getHeader")
    @ResponseBody
    public Object getGetHeader(HttpServletRequest request) {
        return RequestUtils.getRequestHeaders(request);
    }

    @PostMapping(value = "/getHeader")
    @ResponseBody
    public Object getPostHeader(HttpServletRequest request) {
        return RequestUtils.getRequestHeaders(request);
    }
}
